import eventBus from '/@/utils/eventBus';

//当前file处理工具函数,对url拼接
const getCompleteFileArray = (filePathList = []) => {
	let filePush = [];
	for (let index = 0; index < filePathList?.length; index++) {
		const element = filePathList[index];
		let newE = JSON.parse(JSON.stringify(element));
		newE.path = window.$host + '/' + newE.path;
		let parts = newE.path.split('/');
		let filename = parts.pop();
		if (!newE.name) {
			newE.name = filename;
		}
		if (!newE.fileType) {
			newE.fileType = filename.split('.').pop();
		}
		filePush.push(newE);
	}
	return filePush;
};
//删除成功,前端移除数组中对应ids元素(因为后端没有设计单独附件接口, 所以删除成功后,前端手动删除对应id数据)
const removeData = (originalArray: any, idArray: any) => {
	for (let i = originalArray.length - 1; i >= 0; i--) {
		if (idArray.includes(originalArray[i].id)) {
			originalArray.splice(i, 1);
		}
	}
	eventBus.emit('getData');
	return originalArray.length;
};

const isImagePath = (path: any) => {
	return /\.(jpg|jpeg|png|gif)$/i.test(path);
};

export { getCompleteFileArray, removeData,isImagePath };
